package com.myschool.server.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.myschool.common.exception.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.myschool.server.manager.GradeManager;
import com.myschool.server.manager.SectionManager;
import com.myschool.server.manager.SubjectMasterManager;
import com.myschool.server.model.Grade;
import com.myschool.server.model.Json;
import com.myschool.server.model.Section;
import com.myschool.server.model.Subject;
import com.myschool.server.model.SubjectMaster;
import com.myschool.server.web.command.UserSession;

@Controller
public class SectionSubjectController {

	@Autowired
	SubjectMasterManager subjectMasterManager;

	@Autowired
	GradeManager gradeManager;

	@Autowired
	SectionManager sectionManager;

	@RequestMapping("loadGradeSubjects.htm")
	public 
	ModelAndView retrieveAllGrades(HttpServletRequest request) {

		/**
		 * String school = request.getParameter("schoolId"); Long schoolId = new
		 * Long(1L); if (school != null) { schoolId = Long.parseLong(school); }
		 * UserSession userSession = (UserSession) request.getSession()
		 * .getAttribute(UserSession.CURRENT_USER); Set<Grade> grades =
		 * this.gradeManager .retrieveAllGrades(userSession != null ?
		 * userSession .getSchoolId() : schoolId);
		 **/
		List<Grade> grades = new ArrayList<Grade>();
		Grade grade1 = new Grade();
		grade1.setEntityId(11L);
		grade1.setGradeName("first");
		grade1.setGradeNumber(1L);
		grades.add(grade1);

		Grade grade2 = new Grade();
		grade2.setEntityId(12L);
		grade2.setGradeName("second");
		grade2.setGradeNumber(2L);
		grades.add(grade2);

		ModelAndView model = new ModelAndView("sectionSubjects");

		model.addObject("grades", grades);

		model.addObject("section", new Section());
		System.out.println("hiiiiiiiiiiiiiiii");
		return model;
	}

	@RequestMapping("getSubjectSectionList.htm")
	public @ResponseBody
	ModelMap retrieveGradeSections(HttpServletRequest request,
			@RequestParam Long gradeId) {

		Grade grade = null;
		try {
			grade = this.gradeManager.retrieve(gradeId);
		} catch (ApplicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelMap model = new ModelMap();
		model.addAttribute("grade", grade);

		return model;
	}

	@RequestMapping("getMasterSubjectsForSections.htm")
	public @ResponseBody
	Json<SubjectMaster> retrieveAllSubjects(HttpServletRequest request) {

		Json<SubjectMaster> jsonData = new Json<SubjectMaster>();

		List<SubjectMaster> subjects = subjectMasterManager.retrieveAll();

		jsonData.setPage(1);
		jsonData.setRecord(2);
		jsonData.setTotal(2);
		jsonData.setRowList(subjects);
		return jsonData;
	}

	@RequestMapping("addSectionSubjects.htm")
	public @ResponseBody
	ModelMap addSectionSubjects(HttpServletRequest request,
			List<Subject> subjects) {
		ModelMap map = new ModelMap();
		// try {
		map.addAttribute("message", "Subject added successfully");
		/*
		 * } catch (ApplicationException e) { map.addAttribute("message",
		 * "Error during subject save."); }
		 */
		return map;

	}

	@RequestMapping("deleteSectionSubjects.htm")
	public @ResponseBody
	ModelMap deleteSubjects(HttpServletRequest request) {
		ModelMap map = new ModelMap();
		String deleteIds = request.getParameter("deleteIds");
		boolean isError = false;
		String[] ids = deleteIds.split(";");
		List<String> delIds = new ArrayList<String>();
		for (String id : ids) {
			try {
				Long entityId = Long.parseLong(id);
				this.subjectMasterManager.delete(entityId);
				delIds.add(id);
			} catch (NumberFormatException numExp) {
				numExp.printStackTrace();
				isError = true;
				// id is not right
			} catch (ApplicationException e) {
				e.printStackTrace();
				isError = true;
				// map.addAttribute("message", "Error during subject delete.");
			}

		}
		if (isError) {
			map.addAttribute("message", "Error during Subjects deletion");
		} else {
			map.addAttribute("message", "Subjects deleted successfully");
		}
		map.addAttribute("deletedItems", delIds);
		return map;

	}

	/**
	 * @return the subjectMasterManager
	 */
	public SubjectMasterManager getSubjectMasterManager() {
		return subjectMasterManager;
	}

	/**
	 * @param subjectMasterManager
	 *            the subjectMasterManager to set
	 */
	public void setSubjectMasterManager(
			SubjectMasterManager subjectMasterManager) {
		this.subjectMasterManager = subjectMasterManager;
	}

	/**
	 * @return the gradeManager
	 */
	public GradeManager getGradeManager() {
		return gradeManager;
	}

	/**
	 * @param gradeManager
	 *            the gradeManager to set
	 */
	public void setGradeManager(GradeManager gradeManager) {
		this.gradeManager = gradeManager;
	}

	/**
	 * @return the sectionManager
	 */
	public SectionManager getSectionManager() {
		return sectionManager;
	}

	/**
	 * @param sectionManager
	 *            the sectionManager to set
	 */
	public void setSectionManager(SectionManager sectionManager) {
		this.sectionManager = sectionManager;
	}

}
